<template>
	<view>
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="欢迎进入风动社区"></u-loading-page>
		<view class="main">
			
		</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				loadding: true,
				openid: '',
				customerinfo: {},
				wxInfo: {}
			}
		},
		onLoad(e) {
			this.customerinfo = uni.getStorageSync('customerinfo');
			this.openid = uni.getStorageSync('openid');
			if (this.openid == '' || this.openid == undefined) {
				if (e.code) {
					this.getWxOpenId(e.code)
				} else {
					this.getWxConfigInfo(e.code)
				}
			} else {
				this._login()
			}
		},
		methods: {
			// 登录
			async _login() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Login/cus_login',
					data: {
						openid: this.openid
					}
				});
				uni.hideLoading()
				this.loadding = false
				if (res.code == 1) {
					this.userinfo = res.data.userinfo
					uni.setStorageSync('customerinfo', res.data.userinfo);
					uni.navigateTo({
						url: '/pages/customer/index'
					})
				}
			},
			// 获取微信公众号信息
			async getWxConfigInfo() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Wechats/getWxConfigInfo'
				});
				if (res.code == 1) {
					uni.setStorageSync('wxInfo', res.data);
					this.getCodes('')
				}
			},
			// 获取CODE
			getCodes(code) {
				if (code == '' || code == undefined) {
					this.wxInfo = uni.getStorageSync('wxInfo');
					const redirect_url = util.WEB_URL + '/pages/merchant/index';
					const url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.wxInfo.AppID +
						'&redirect_uri=' + encodeURIComponent(redirect_url) +
						'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
					window.location.href = url
				} else {
					this.getWxOpenId(code)
				}
			},
			// 获取微信OPENID
			async getWxOpenId(code) {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Wechats/getWxOpenId',
					data: {
						code: code
					}
				});
				if (res.code == 1) {
					if (res.data != null && res.data != '' && res.data != undefined) {
						this.openid = res.data
						uni.setStorageSync('openid', res.data);
						this._login()
					}
				}
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
